<template>
    <div>
        <iview-table
                ref="table"
                :span1="19"
                :span2="5"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,changeSelect:changeSelect,events:events}">
        </iview-table>
        <br>
        <iview-page
                style="margin-bottom: 200px"
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
        <Modal v-model="showAdd" @on-ok="save" ok-text="保存" :closable='false' :mask-closable=true :width="800">
            <h3 slot="header" style="color:#2D8CF0">实验结果</h3>
            <Form :label-width="140">
                <Row justify="center" class="code-row-bg">
                    <Col span="22" offset="1">
                        <FormItem label="皮肤点刺实验结果：">
                            <Input v-model="formItem.skinPrickExperiment" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row justify="center" class="code-row-bg">
                    <Col span="22" offset="1">
                        <FormItem label="血清IgE实验结果：">
                            <Input v-model="formItem.serumLgeExperiment" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row justify="center" class="code-row-bg">
                    <Col span="22" offset="1">
                        <FormItem label="补充内容：">
                            <Input v-model="formItem.supplementContent" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row justify="center" class="code-row-bg">
                    <Col span="22" offset="1">
                        <FormItem label="实验员：">
                            <Input v-model="formItem.experimenter" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                    </Col>
                </Row>
                <Row justify="center" class="code-row-bg">
                    <Col span="22" offset="1">
                        <FormItem label="诊断医生：">
                            <Input v-model="formItem.doctor" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        </Modal>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage
        },
        data () {
            return {
                baseURL: '/user/allergen',
                showAdd: false,
                selectRows: [],
                formItem: {
                    id: '',
                    patientId: '',
                    complaintId: '',
                    skinPrickExperiment: '',
                    serumLgeExperiment: '',
                    supplementContent: '',
                    experimenter: '',
                    doctor: ''
                },
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/allergen',
                    defaultFilters: [
                        {'field': 'patient_id', 'value1': -1, 'operate': '=='},
                        {'field': 'complaint_id', 'value1': -1, 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 6315,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },
                        {
                            title: '皮肤点刺实验',
                            key: 'skinPrickExperiment'
                        },
                        {
                            title: '血清IgE实验',
                            key: 'serumLgeExperiment'
                        },
                        {
                            title: '补充内容',
                            key: 'supplementContent'
                        },
                        {
                            title: '实验员',
                            key: 'experimenter',
                        },
                        {
                            title: '诊断医师',
                            key: 'doctor',
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                }
            };
        },
        methods: {
            events (item) {
                let path = item.path
                if (path === 'add') {
                    this.add()
                } else if (path === 'edit') {
                    this.edit()
                } else if (path === 'delete') {
                    this.delete()
                }
            },
            add () {
                this.formItem.id = ''
                this.$bus.init(this.formItem)
                this.showAdd = true
            },
            save () {
                this.formItem.patientId = this.$store.state.app.patientId
                this.formItem.complaintId = this.$store.state.app.complaintId
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '保存成功')
                        this.showAdd = false
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            edit () {
                if (this.selectRows.length === 1) {
                    this.formItem = this.selectRows[0]
                    this.showAdd = true
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            delete () {
                if (this.selectRows.length === 1) {
                    let ids = []
                    for (let row of this.selectRows) {
                        ids.push(row.id)
                    }
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除'+ids.length+'条记录?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            doDelete (ids) {
                let url = this.baseURL + '/delete'
                this.$ajax.post(url, ids).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '删除成功')
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            refresh () {
                this.queryModel.defaultFilters.length = 0
                if (this.$store.state.app.patientId) {
                    this.queryModel.defaultFilters.push({'field': 'patient_id', 'value1': this.$store.state.app.patientId, 'operate': '=='})
                }
                if (this.$store.state.app.complaintId) {
                    this.queryModel.defaultFilters.push({'field': 'complaint_id', 'value1': this.$store.state.app.complaintId, 'operate': '=='})
                }
                this.$refs.table.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
                this.refresh()
            }
        },
        mounted () {
            // this.init()
        }
        // ,
        // watch: {
        //     '$route' (to) {
        //         if (to.path === '/course') {
        //             this.init()
        //         }
        //     }
        // }
    }
</script>

<style>

</style>
